/**
 * 获取省份
 */
function get_province(){
    var url = '/index.php?m=admin&c=Freight&a=getRegion&level=1&parent_id=0';
    $.ajax({
        type : "GET",
        url  : url,
        error: function(request) {
            alert("服务器繁忙, 请联系管理员!");
            return;
        },
        success: function(v) {
            v = '<option value="0">选择省份</option>'+ v;
            $('#province').empty().html(v);
        }
    });
}


/**
 * 获取城市
 * @param t  省份select对象
 */
function get_city(t){
    var parent_id = $(t).val();
    if(!parent_id > 0){
        return;
    }
    $('#district').empty().css('display','none');
    $('#twon').empty().css('display','none');
    var url = '/index.php?m=admin&c=Freight&a=getRegion&level=2&parent_id='+ parent_id;
    $.ajax({
        type : "GET",
        url  : url,
        error: function(request) {
            alert("服务器繁忙, 请联系管理员!");
            return;
        },
        success: function(v) {
            v = '<option value="0">选择城市</option>'+ v;
            $('#city').empty().html(v);
        }
    });
}

/**
 * 获取地区
 * @param t  城市select对象
 */
function get_area(t){
    var parent_id = $(t).val();
    if(!parent_id > 0){
        return;
    }
    $('#district').empty().css('display','inline');
    $('#twon').empty().css('display','none');
    var url = '/index.php?m=admin&c=Freight&a=getRegion&level=3&parent_id='+ parent_id;
    $.ajax({
        type : "GET",
        url  : url,
        error: function(request) {
            alert("服务器繁忙, 请联系管理员!");
            return;
        },
        success: function(v) {
            v = '<option>选择区域</option>'+ v;
            $('#district').empty().html(v);
        }
    });
}

// 获取最后一级乡镇
function get_twon(obj){
    var parent_id = $(obj).val();
    var url = '/index.php?m=admin&c=Freight&a=getTwon&parent_id='+ parent_id;
    $.ajax({
        type : "GET",
        url  : url,
        success: function(res) {
            if(parseInt(res) == 0){
                $('#twon').empty().css('display','none');
            }else{
                $('#twon').css('display','inline');
                $('#twon').empty().html(res);
            }
        }
    });
}


/**
 * 输入为空检查
 * @param name '#id' '.id'  (name模式直接写名称)
 * @param type 类型  0 默认是id或者class方式 1 name='X'模式
 */
function is_empty(name,type){
    if(type == 1){
        if($('input[name="'+name+'"]').val() == ''){
            return true;
        }
    }else{
        if($(name).val() == ''){
            return true;
        }
    }
    return false;
}

/**
 * 手机号码格式判断
 * @param tel
 * @returns {boolean}
 */
function checkMobile(tel) {
    var reg = /(^1[3|4|5|6|7|8|9][0-9]{9}$)/;
    //var reg = /^1[0-9]{10}$/;
    if (reg.test(tel)) {
        return true;
    }else{
        return false;
    };
}

/*
 * 上传图片 后台专用
 * @access  public
 * @null int 一次上传图片张图
 * @elementid string 上传成功后返回路径插入指定ID元素内
 * @path  string 指定上传保存文件夹,默认存在public/upload/temp/目录
 * @callback string  回调函数(单张图片返回保存路径字符串，多张则为路径数组 )
 */
function GetUploadify(num,elementid,path,callback,fileType)
{


    var upurl ='/index.php?m=admin&c=Uploadify&a=upload&num='+num+'&input='+elementid+'&path='+path+'&func='+callback+'&fileType='+fileType;
    var title = '上传图片';
    if(fileType == 'Flash'){
        title = '上传视频';
    }
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: false,
        maxmin: true, //开启最大化最小化按钮
        area: ['50%', '60%'],
        content: upurl
     });
}

/*
 * 上传图片 前台专用
 * @access  public
 * @null int 一次上传图片张图
 * @elementid string 上传成功后返回路径插入指定ID元素内
 * @path  string 指定上传保存文件夹,默认存在public/upload/temp/目录
 * @callback string  回调函数(单张图片返回保存路径字符串，多张则为路径数组 )
 */
function GetUploadify2(num,elementid,path,callback)
{	   	
	var upurl ='/index.php?m=Home&c=Uploadify&a=upload&num='+num+'&input='+elementid+'&path='+path+'&func='+callback;
    layer.open({
        type: 2,
        title: '上传图片',
        shadeClose: true,
        shade: false,
        maxmin: true, //开启最大化最小化按钮
        area: ['50%', '60%'],
        content: upurl
     });
}
/*
 * 删除组图input
 * @access   public
 * @val  string  删除的图片input
 */
function ClearPicArr(val)
{
	$("li[rel='"+ val +"']").remove();
	$.get(
		"{:U('Uploadify/delupload')}",{action:"del", filename:val},function(){}
	);
}
/*
 * 删除组图input
 * @access   public
 * @val  string  删除的图片input
 */
function ClearPicArr2(val)
{
    $("li[rel='"+ val +"']").remove();
    $.get(
        "{:U('Home/Uploadify/delupload')}",{action:"del", filename:val},function(){}
    );
}
	
// 获取活动剩余天数 小时 分钟
//倒计时js代码精确到时分秒，使用方法：注意 var EndTime= new Date('2013/05/1 10:00:00'); //截止时间 这一句，特别是 '2013/05/1 10:00:00' 这个js日期格式一定要注意，否则在IE6、7下工作计算不正确哦。
//js代码如下：
function GetRTime(end_time){
      // var EndTime= new Date('2016/05/1 10:00:00'); //截止时间 前端路上 http://www.51xuediannao.com/qd63/
	   var EndTime= new Date(end_time); //截止时间 前端路上 http://www.51xuediannao.com/qd63/
       var NowTime = new Date();
       var t =EndTime.getTime() - NowTime.getTime();
       /*var d=Math.floor(t/1000/60/60/24);
       t-=d*(1000*60*60*24);
       var h=Math.floor(t/1000/60/60);
       t-=h*60*60*1000;
       var m=Math.floor(t/1000/60);
       t-=m*60*1000;
       var s=Math.floor(t/1000);*/

       var d=Math.floor(t/1000/60/60/24);
       var h=Math.floor(t/1000/60/60%24);
       var m=Math.floor(t/1000/60%60);
       var s=Math.floor(t/1000%60);
	   if(s >= 0)	
	   return d + '天' + h + '小时' + m + '分' +s+'秒';
   }
   
   
/**
 * 获取多级联动的商品分类
 */
function get_category(id,next,select_id){
    $.ajax({
        type : "GET",
        url  : '/index.php?m=Mobile&c=Api&a=get_category&parent_id='+ id,
        dataType:'json',
        success: function(data) {
			var html = "<option value='0'>请选择商品分类</option>";
            for (var i=0 ;i<data.result.length;i++){
                html+= "<option value='"+data.result[i].id+"'>"+data.result[i].name+"</option>";
            }
            $('#'+next).empty().html(html);
			(select_id > 0) && $('#'+next).val(select_id);//默认选中
        }
    });
}

// 读取 cookie
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
	  c_start = document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
	  { 
	    c_start=c_start + c_name.length+1 
	    c_end=document.cookie.indexOf(";",c_start)
	    if (c_end==-1) c_end=document.cookie.length
	    	return unescape(document.cookie.substring(c_start,c_end))
	  } 
	}
	return "";
}

function setCookies(name, value, time)
{
	var cookieString = name + "=" + escape(value) + ";";
	if (time != 0) {
		var Times = new Date();
		Times.setTime(Times.getTime() + time);
		cookieString += "expires="+Times.toGMTString()+";"
	}
	document.cookie = cookieString+"path=/";
}
function delCookie(name){
    var exp=new Date();
    exp.setTime(exp.getTime()-1);
    var cval=getCookie(name);
    if(cval!=null){
        document.cookie=name+"="+cval+";expires="+exp.toGMTString() +"path=/";
    }
}

function layConfirm(msg , callback){
	layer.confirm(msg, {
		  btn: ['确定','取消'] //按钮
		}, function(){
			callback();
			layer.closeAll();
		}, function(index){
			layer.close(index);
			return false;// 取消
		}
	);
}

function isMobile(){
	return "yes";
}

// 判断是否手机浏览器
function isMobileBrowser()
{
    var sUserAgent = navigator.userAgent.toLowerCase();    
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";    
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";    
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";    
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";    
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";    
    var bIsAndroid = sUserAgent.match(/android/i) == "android";    
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";    
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";    
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM ){    
        return true;
    }else 
	    return false;
}

function getCookieByName(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1)
        return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1)
        end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}
function showErrorMsg(msg){
    layer.open({content:msg,time:2});
}
//关闭页面
function CloseWebPage(){
    if (navigator.userAgent.indexOf("MSIE") > 0) {
        if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
            window.opener = null;
            window.close();
        } else {
            window.open('', '_top');
            window.top.close();
        }
    }
    else if (navigator.userAgent.indexOf("Firefox") > -1 || navigator.userAgent.indexOf("Chrome") > -1) {
        window.location.href = 'about:blank';
    } else {
        window.opener = null;
        window.open('', '_self', '');
        window.close();
    }
}

//时间戳转换
function add0(m){return m<10?'0'+m:m }
function  formatDate(now)   {
    var time = new Date(now);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'/'+add0(m)+'/'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function round(x, num){
    return Math.round(x * Math.pow(10, num)) / Math.pow(10, num) ;
}

// 验证码切换
function verify(){
    $('#verify_code_img').attr('src','/index.php?m=Mobile&c=index&a=verify&r='+Math.random());
}

function date(time){
    return new Date(parseInt(time) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}

//显示大图片
function showBigImage(e) {
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true, //点击阴影关闭
        //area: [$(e).width + 'px', $(e).height + 'px'], //宽高
        area: ['400px', '400px'], //宽高
        content: "<img src=" + $(e).attr('src') + " />"
    });
}

function urlEncode(param, key, encode) {
    if (param==null) return '';
    var paramStr = '';
    var t = typeof (param);
    if (t == 'string' || t == 'number' || t == 'boolean') {
        paramStr += '/' + key + '/'  + ((encode==null||encode) ? encodeURIComponent(param) : param);
    } else {
        for (var i in param) {
            var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
            paramStr += urlEncode(param[i], k, encode)
        }
    }
    return paramStr;
}

function uploader(obj,multi,tabs,other_param=''){
    if(!multi) multi = false
    if(!tabs){
        tabs = {'browser':'active','selecticon':''};
    }
    var maxwidth = 750;
    if($(obj).attr('maxwidth')){
        maxwidth = $(obj).attr('maxwidth')
    }
    var maxheight = 1080;
    if($(obj).attr('maxheight')){
        maxheight = $(obj).attr('maxheight')
    }
    fileUploader.show(function(imgs,ret) {
        console.log(imgs);
        var inputid = $(obj).attr('upload-input');
        var previewid = $(obj).attr('upload-preview');
        var params   = $(obj).attr('upload-params');
        if(!params) params = '';
        if (imgs.length == 0) {
            return;
        } else if (!multi) {
            if(inputid){
                $('#'+inputid).val(imgs.url+params);
                console.log('change')
                $('#'+inputid).change();
            }
            if($('#'+inputid+'_url').length > 0){
                $('#'+inputid+'_url').attr('src',imgs.url+params);
            }
            console.log(imgs.url);
            if($('#'+previewid+' img').length > 0){
                console.log(111);
                $('#'+previewid+' img').attr('src',imgs.url+params);
                $('#'+previewid+' a').show();
                $('#'+previewid+' img').show();
            }else{
                $('#'+previewid).text('已上传');
            }
            if(other_param=='ffmpeg'){
                if(ret){
                    $('#pic').val(ret);
                    $('#picPreview img').attr('src',ret);
                }else{
                    $.post("/index.php?s=/upload/ffmpeg_img",{'path':imgs.url},function(res2){
                        if(res2.status==1 && res2.url){
                            $('#pic').val(res2.url);
                            $('#picPreview img').attr('src',res2.url);
                        }
                    })
                }
            }
        } else {
            if(other_param =='fujian'){
                for (i in imgs) {

                    var boxstyle = $(obj).attr('upload-style');
                    var previewtpl = '<div class="fj-module" style="position: relative;margin-bottom: 10px;'+boxstyle+'">'
                    previewtpl += '<a  style="right: -20px" class="layui-imgbox-close" href="javascript:void(0)" onclick="$(this).parent().remove();getfjpicsval(\''+inputid+'\',\''+previewid+'\')" title="删除"><i class="layui-icon layui-icon-close-fill"></i></a>';
                    previewtpl += '<a target="_blank" href="'+imgs[i].url+params+'"><p class="fjfile" style="display: block; width: 100%;height: 100%;padding: 7px;border: #d3d3d3 1px solid;">';
                    previewtpl += ''+imgs[i].url+params+'';
                    previewtpl += '</p></a>';
                    previewtpl += '</div>';
                    $('#'+previewid).append(previewtpl);
                    getfjpicsval(inputid,previewid);
                }

            }else{
                var imglist = [];
                for (i in imgs) {
                    var boxstyle = $(obj).attr('upload-style');
                    var previewtpl = '<div class="layui-imgbox" style="'+boxstyle+'">'
                    previewtpl += '<a class="layui-imgbox-close" href="javascript:void(0)" onclick="$(this).parent().remove();getpicsval(\''+inputid+'\',\''+previewid+'\')" title="删除"><i class="layui-icon layui-icon-close-fill"></i></a>';
                    previewtpl += '<span class="layui-imgbox-img">';
                    previewtpl += '<img src="'+imgs[i].url+params+'" style="max-height:100%;" onclick="preview(this)">';
                    previewtpl += '</span>';
                    previewtpl += '</div>';
                    $('#'+previewid).append(previewtpl);
                    getpicsval(inputid,previewid);
                }
            }

        }
    },{type: 'image',direct: false,maxheight:maxheight,maxwidth:maxwidth,multi: multi,tabs:tabs,other_param:other_param});
}
function getpicsval(inputid,previewid){
    var pics = []
    $('#'+previewid+' .layui-imgbox-img>img').each(function(){
        if($(this).attr('src'))
            pics.push($(this).attr('src'));
    })
    if(inputid){
        $('#'+inputid).val(pics.join(','));
        $('#'+inputid).change();
    }
}

//弹窗
function dialog(msg, status,url) {
    if(typeof msg == 'object'){
        var res = msg
        msg = res.msg
        status = res.status
        url = res.url
    }
    layer.open({
        //style: 'border:none; background-color:#333; color:#fff;',
        icon:status,
        content: msg,
        end: function() {
            if (typeof(url) != "undefined" && url != "") {
                if(url === true){
                    reload();
                }else{
                    window.location.href = url;
                }
            }
        }
    });
}
function reload(){
    var href = window.location.href;
    href = href.replace(/&?t_reload=(\d+)/g,'')
    window.location.href = href+(href.indexOf('?') > -1?'&':'?')+"t_reload="+new Date().getTime()
}
